//
//  MrLvSegmentView.m
//  AF3
//
//  Created by dzMac on 16/5/8.
//  Copyright © 2016年 lvlinhu. All rights reserved.
//

#import "MrLvSegmentView.h"
#import "Masonry.h"
#import "UIColor+MrLvColor.h"

#define SCREEN_WIDTH  [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define lineScale     (1/[[UIScreen mainScreen] scale])    //线条1px

@interface MrLvSegmentView()

@property (nonatomic, strong) UIView *btnView;//按钮view，放桩位按钮，信息按钮，评论按钮以及按钮下边横线
@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation MrLvSegmentView


- (instancetype)initWithFrame:(CGRect)frame withTitlesArr:(NSMutableArray *)titlesArr iconArr:(NSMutableArray *)iconArr{
    if (self = [super initWithFrame:frame]) {
        self.arrTitle = [[NSMutableArray alloc] init];
        self.buttonArray = [[NSMutableArray alloc] init];
        self.arrTitle = titlesArr;
        self.arrIcon  = iconArr;
                self.arrNomalIcon = [[NSMutableArray alloc] initWithObjects:@"icon_subtab_list",@"icon_subtab_info",@"icon_subtab_review", nil];
        self.textNomalColor = [UIColor colorWithHexString:@"#aaaaaa"];
        self.textSelectedColor = [UIColor colorWithHexString:@"#5e4fff"];
        self.lineColor = [UIColor colorWithHexString:@"#5e4fff"];
        self.titleFont = [UIFont systemFontOfSize:16];
        
        [self createUI];
    }
    
    return self;
}

- (void)createUI {
    self.btnView = [UIView new];
    [self addSubview:self.btnView];
    [self.btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(0);
        make.left.right.equalTo(self);
        make.height.equalTo(@50);
    }];
    
    self.lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, (SCREEN_WIDTH - 2)/self.arrTitle.count, 5)];
    self.lineView.backgroundColor = self.lineColor;
//    self.lineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"login_tab"]];
    self.lineView.image = [UIImage imageNamed:@"login_tab"];
    [self.btnView addSubview:self.lineView];
    [self.btnView bringSubviewToFront:self.lineView];
    //    [self.btnView sendSubviewToBack:self.lineView];
    
    UIView *lineViewTop = [UIView new];
    lineViewTop.backgroundColor = [UIColor colorWithHexString:@"#c9ccd4"];
    [self.btnView addSubview:lineViewTop];
    [lineViewTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.btnView);
        make.height.equalTo(@lineScale);
    }];
    
    // 5.创建多个按钮
    for(NSInteger i = 0;i < self.arrTitle.count; i++){
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.arrTitle[i] forState:UIControlStateNormal];
        btn.tag = i + 1;
        btn.titleLabel.font = self.titleFont;
        [btn setImage:[UIImage imageNamed:self.arrNomalIcon[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:self.arrIcon[i]] forState:UIControlStateSelected];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
        [self.btnView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@40);
            make.width.equalTo(@((SCREEN_WIDTH - 2)/self.arrTitle.count));
            make.left.equalTo(self.btnView).mas_offset( i * ((SCREEN_WIDTH - 2)/self.arrTitle.count));
            make.top.equalTo(self.btnView).mas_offset(@2);
        }];
        
        [btn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttonArray addObject:btn];
        
        UIView *lineone = [UIView new];
        lineone.backgroundColor = [UIColor colorWithHexString:@"#c9ccd4"];
        [self.btnView addSubview:lineone];
        [lineone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@lineScale);
            make.height.equalTo(@(20));
            make.left.equalTo(self).mas_offset( i * ((SCREEN_WIDTH - 2)/self.arrTitle.count));
            make.top.equalTo(self.btnView).mas_offset(@12);
        }];
        
        if (i == 0) {
            btn.selected = YES;
            [btn setTitleColor:self.textSelectedColor forState:UIControlStateNormal];
        }
        else {
            btn.selected = NO;
            [btn setTitleColor:self.textNomalColor forState:UIControlStateNormal];
        }
        
    }
    
    UIView *lineViewDown = [UIView new];
    lineViewDown.backgroundColor = [UIColor colorWithHexString:@"#c9ccd4"];
    [self.btnView addSubview:lineViewDown];
    [lineViewDown mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.btnView);
        make.height.equalTo(@lineScale);
    }];
    
}


#pragma mark - Actions
- (void)touchBtn:(id)sender {
    UIButton *button = (UIButton *)sender;
    if ([self.delegate respondsToSelector:@selector(selectedIndex:)]) {
        [self.delegate selectedIndex:button.tag -1];
    }
    
}

-(void)selectIndex:(NSInteger)index {
    for (UIButton *subButton in self.buttonArray) {
        if (index != subButton.tag -1) {
            subButton.selected = NO;
            [subButton setTitleColor:self.textNomalColor forState:UIControlStateNormal];
        }
        else{
            subButton.selected = YES;
            [subButton setTitleColor:self.textSelectedColor forState:UIControlStateNormal];
            [UIView animateWithDuration:0.3 animations:^{
                self.lineView.frame = CGRectMake(index*((SCREEN_WIDTH - 2)/self.arrTitle.count), 45, (SCREEN_WIDTH - 2)/self.arrTitle.count, 5);
            }];
        }
    }
}

#pragma mark -- set
-(void)setLineColor:(UIColor *)lineColor{
    if (_lineColor != lineColor) {
        self.lineView.backgroundColor = lineColor;
        _lineColor = lineColor;
    }
}
-(void)setTextNomalColor:(UIColor *)textNomalColor{
    if (_textNomalColor != textNomalColor) {
        for (UIButton *subButton in self.buttonArray){
            [subButton setTitleColor:textNomalColor forState:UIControlStateNormal];
        }
        _textNomalColor = textNomalColor;
    }
}
-(void)setTextSelectedColor:(UIColor *)textSelectedColor{
    if (_textSelectedColor != textSelectedColor) {
        for (UIButton *subButton in self.buttonArray){
            [subButton setTitleColor:textSelectedColor forState:UIControlStateSelected];
        }
        _textSelectedColor = textSelectedColor;
    }
}
-(void)setTitleFont:(UIFont *)titleFonts{
    if (_titleFont != titleFonts) {
        for (UIButton *subButton in self.buttonArray){
            subButton.titleLabel.font = titleFonts;
        }
        _titleFont = titleFonts;
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */


@end
